/************************************************************************************
 *  Copyright 2013 WZITech Corporation. All rights reserved.
 *	
 *	模	块：		CurrentUserSession
 *	包	名：		com.wzitech.chinabeauty.facade.frontend
 *	项目名称：	chinabeauty-facade-frontend
 *	作	者：		SunChengfei
 *	创建时间：	2013-9-27
 *	描	述：		
 *	更新纪录：	1. SunChengfei 创建于 2013-9-27 上午10:30:22
 * 				
 ************************************************************************************/
package com.hn658.base.message.frontend.context;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.NamedThreadLocal;

import com.hn658.base.message.account.entity.AccountUserInfoEO;
import com.hn658.framework.shared.entity.IUser;
import com.hn658.framework.shared.exception.UserNotLoginException;


/**
 * 当前线程用户信息类
 * @author SunChengfei
 *
 */
public class CurrentUserContext {
	private static final ThreadLocal<RequestContext> CURRENT_REQUEST_CONTEXT_THREAD_LOCAL = new NamedThreadLocal<RequestContext>("hn658-request-context");

    /**
     * 获取当前登录帐号
     *
     * @return
     */
    public static String getLoginAccount() {
    	RequestContext requestContext = CURRENT_REQUEST_CONTEXT_THREAD_LOCAL.get();
        if(requestContext == null){
            return null;
        }
        IUser userInfo = requestContext.getUserInfo();
        return (userInfo != null ? userInfo.getLoginAccount() : null);
    }

    /**
     * 获取当前登录Uid
     * @return
     */
    public static Long getUid() {
    	RequestContext requestContext = CURRENT_REQUEST_CONTEXT_THREAD_LOCAL.get();
        if(requestContext == null){
            return null;
        }
        return requestContext.getUid();
    }

    /**
     * 获取当前请求签名
     * @return
     */
    public static String getRequestSign() {
    	RequestContext currentContext = CURRENT_REQUEST_CONTEXT_THREAD_LOCAL.get();
        if(currentContext == null){
            return null;
        }
        return currentContext.getSign();
    }

    /**
     * 获取当前请求timestamp
     * @return
     */
    public static String getRequestTimestamp() {
    	RequestContext requestContext = CURRENT_REQUEST_CONTEXT_THREAD_LOCAL.get();
        if(requestContext == null){
            return null;
        }
        return requestContext.getTimestamp();
    }

    /**
     * 获取当前用户对应authkey
     * @return
     */
    public static String getUserAuthkey() {
    	RequestContext requestContext = CURRENT_REQUEST_CONTEXT_THREAD_LOCAL.get();
        if(requestContext == null){
            return null;
        }
        return requestContext.getToken();
    }

    /**
     * 获取当前设备号deviceId
     * @return
     */
    public static String getRequestDeviceId(){
    	RequestContext sessionInfo = CURRENT_REQUEST_CONTEXT_THREAD_LOCAL.get();
        if(sessionInfo == null){
            return null;
        }
        return sessionInfo.getDeviceId();
    }
    
    /**
     * 获取当前设备类型
     * @return
     */
    public static Integer getRequestDeviceType(){
    	RequestContext sessionInfo = CURRENT_REQUEST_CONTEXT_THREAD_LOCAL.get();
        if(sessionInfo == null){
            return null;
        }
        return sessionInfo.getDeviceType();
    }

    /**
     * 返回当前用户
     * @return
     */
    public static IUser getUser(){
    	RequestContext sessionInfo = CURRENT_REQUEST_CONTEXT_THREAD_LOCAL.get();
        if(sessionInfo == null){
            throw new UserNotLoginException();
        }
        return sessionInfo.getUserInfo();
    }
    
    /**
     * 返回当前用户写真实IP
     * @return
     */
    public static String getIp(){
    	RequestContext requestContext = CURRENT_REQUEST_CONTEXT_THREAD_LOCAL.get();
        if(requestContext == null){
            throw null;
        }
        return requestContext.getIp();
    }
    
    public static void setUser(IUser user){
    	RequestContext sessionInfo = CURRENT_REQUEST_CONTEXT_THREAD_LOCAL.get();
        if(sessionInfo == null){
            return;
        }
        sessionInfo.setUserInfo((AccountUserInfoEO)user);
    }

    /**
     * 设置当前请求上下文信息
     * @param requestContext
     */
    public static void setRequestContext(RequestContext requestContext) {
        CURRENT_REQUEST_CONTEXT_THREAD_LOCAL.set(requestContext);
    }

    /**
     * 获取当前请求上下文信息
     *
     * @return
     */
    public static RequestContext getRequestContext() {
        return CURRENT_REQUEST_CONTEXT_THREAD_LOCAL.get();
    }

    /**
     * 判断当前线程是否登录
     * @return
     */
    public static boolean isSignedIn() {
        return StringUtils.isNotBlank(getLoginAccount());
    }

    /**
     * 清楚当前线程用户信息
     */
    public static void clean() {
        CURRENT_REQUEST_CONTEXT_THREAD_LOCAL.set(null);
    }
}
